﻿using ChholCmdPort;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChholConfig.UserSerial {
	public partial class UsrUartTopic : PortUserForm {
		public UsrUartTopic() : base() {
			InitializeComponent();
		}
		UserSerialPort user_sp_center;
		public UsrUartTopic(RecvFanOut fan_out, Form main_form) 
			:base(global::ChholConfig.Properties.Resources.rs_232_female_30px,16, fan_out,"Usr串口") {
			InitializeComponent();
			user_sp_center = new UserSerialPort(this.Port_user.sendPkg);
			user_sp_center.eCreateRemoteForm += User_sp_center_eCreateRemoteForm;
			Port_user.dRecvPkg = user_sp_center.recvPkg;
			user_sp_center.main_form = main_form;
			myTS.Enabled = false;
		}
		protected override void turnOn() {
			myTS.Enabled = true;
		}
		protected override void OnClosed(EventArgs e) {
			base.OnClosed(e);
			this.Port_user.stop();
			user_sp_center.Dispose();
		}

		private void User_sp_center_eCreateRemoteForm(object sender, CreateRemoteFormEventArgs e) {
			int i = this.RemoteDGV.Rows.Add();
			RemoteDGV[0,i].Value = e.Addr.ToString("x4");
			e.Created_form.dUpdateSpeed += speed => RemoteDGV[1,i].Value = speed;
		}

		private void AddPortBT_Click(object sender, EventArgs e) {
			user_sp_center.createRemoteDuplex((ushort)AddrTB.Num);
		}
	}
}
